package com.test.reggieadmin.model.fragment.mycenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.lxj.xpopup.XPopup;
import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseFragment;
import com.test.reggieadmin.bean.BaseBean;
import com.test.reggieadmin.bean.EmployeeBean;
import com.test.reggieadmin.bean.EmployeeDetailsBean;
import com.test.reggieadmin.bean.State;
import com.test.reggieadmin.databinding.FragmentMyCenterBinding;
import com.test.reggieadmin.http.API;
import com.test.reggieadmin.model.ip.IPSettingActivity;
import com.test.reggieadmin.model.login.LoginActivity;
import com.yechaoa.yutils.GsonUtil;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

/**
 * 个人中心
 */
public class MyCenterFragment extends BaseFragment<FragmentMyCenterBinding, MyCenterPrensenter>
        implements MyCenterView, View.OnClickListener {

    private EmployeeBean employeeBean;
    private int SET = 201;

    @Override
    protected MyCenterPrensenter createPresenter() {
        return new MyCenterPrensenter(this);
    }

    @Override
    protected void initView() {
        viewBinding.btnLogout.setOnClickListener(this);
        viewBinding.llSettingIp.setOnClickListener(this);

        employeeBean = GsonUtil.GsonToBean(String.valueOf(API.kv.decodeString(State.employee)),
                EmployeeBean.class);

        YUtils.showLoading("加载中...");
        presenter.employeeDetails(employeeBean.getData().getId());//根据id查询员工信息
    }

    @Override
    protected void initData() {

    }

    @Override
    public void logoutData(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            //移除登录状态与用户信息
            String[] remove = new String[]{State.isLogin, State.employee};
            API.kv.removeValuesForKeys(remove);
            //到登录页
            startActivity(new Intent(getActivity(), LoginActivity.class));
            getActivity().finish();
        }
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void employeeDetailsData(EmployeeDetailsBean bean) {
        YUtils.hideLoading();
        if (bean.getCode() == 200) {
            viewBinding.tvUsername.setText(bean.getData().getUsername() + "\t" +
                    bean.getData().getName() + "-" +
                    ("1".equals(bean.getData().getSex()) ? "男" : "女"));
        } else {
            ToastUtil.showCenter(bean.getMsg());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == SET && resultCode == Activity.RESULT_OK){
            //退出程序并杀死进程
            ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
            manager.restartPackage("com.test.reggieadmin");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_setting_ip://设置ip
                Intent intent = new Intent(getActivity(), IPSettingActivity.class);
                startActivityForResult(intent,SET);
                break;
            case R.id.btn_logout://退出登录
                new XPopup.Builder(getActivity()).asConfirm("提示", "确定退出登录吗",
                        () -> {
                            presenter.logout();
                        }).show();
                break;
            default:
                break;
        }
    }
}